package com.iasku.iaskueleclass_phone.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.iasku.iaskueleclass_phone.R;
import com.iasku.iaskueleclass_phone.Status;

public class BaseActivity extends FragmentActivity {
    
    public ProgressDialog mProgressDialog;
    public ImageButton mBarLeftBackBtn;
    public TextView mBarCenterTitleTv;
    public Button mBarRightBtn;
    
    public void initProgressDialog(){
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("正在加载...");
    }
    
    public void initTitleBar(){
        initProgressDialog();
        mBarLeftBackBtn = (ImageButton) findViewById(R.id.common_titlebar_back_btn);
        mBarCenterTitleTv = (TextView) findViewById(R.id.common_titlebar_question_title_tv);
        mBarRightBtn = (Button) findViewById(R.id.common_titlebar_right_btn_clear);
        mBarLeftBackBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    
    public void initTitleBar(String titleText){
        initTitleBar();
        mBarCenterTitleTv.setText(titleText);
    }
    
    @Override
    protected void onCreate(Bundle saveInstance) {
        // TODO Auto-generated method stub
        super.onCreate(saveInstance);
    }
    
    @SuppressLint("HandlerLeak")
    public Handler mHander = new Handler() {

        public void handleMessage(Message msg) {
            onTaskComplete(msg.what, (Status) msg.obj);
        }
    };

    public int startTask(int taskId) {
        return startTask(taskId, null);
    }

    public int startTask(int taskId, Bundle bundle) {
        onTaskStart(taskId, bundle);
        new Thread(new TaskRunnable(taskId, bundle)).start();
        return taskId;
    }

    public int onTaskStart(int taskId, Bundle bundle) {
        return taskId;
    }

    /**
     * 后台运行
     * 
     * @param taskId
     *            任务ID
     * @param msg
     * @return error code
     */
    public Status onTaskInBackground(int taskId, Bundle bundle) {
        return new Status(0);
    }

    /**
     * 任务结束，UI线程操作
     * 
     * @param taskId
     * @param err
     *            errorCode
     * @return
     */
    public int onTaskComplete(int taskId, Status status) {
        return taskId;
    }

    class TaskRunnable implements Runnable {

        private int taskId;
        private Bundle bundle;

        public TaskRunnable(int id) {
            taskId = id;
        }

        public TaskRunnable(int id, Bundle m) {
            taskId = id;
            bundle = m;
        }

        @Override
        public void run() {
            Status result = onTaskInBackground(taskId, bundle);
            Message msg = mHander.obtainMessage(taskId);
            msg.obj = result;
            msg.sendToTarget();
        }

    }
}
